package com.mjk.common.util;

import com.mjk.common.tools.encrypt.AESUtil;
import com.mjk.common.tools.logger.BizLogger;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Date;

public class LicenseUtils {
    private static String license;

    static {
        license = initLicense();
    }

    /**
     * 验证证书是否有效
     *
     * @return
     */
    public static boolean checkLicense() {
        if (ObjectUtils.isEmpty(license)) {
            return false;
        }
        try {
            String decry = AESUtil.decrypt(license, AESUtil.INIT_VECTOR);
            String[] names = decry.split(":");
            //判断是否是同一台电脑
            if (!ComputerUtils.getMacAddress().equals(names[1])) {
                return false;
            }
            Date endTime = TimeUtils.parse(TimeUtils.DATE_FORMAT_YEARDATEANDMON, names[3]);
            if (System.currentTimeMillis() - endTime.getTime() > 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 生成授权码
     *
     * @return
     */
    public static String getReqCode() {
        String waitLicense = ComputerUtils.getHostName() + ":" + ComputerUtils.getMacAddress() + ":" + TimeUtils.formatTime(new Date(), TimeUtils.DATE_FORMAT_YEARDATEANDMON);
        String license = AESUtil.encrypt(waitLicense, AESUtil.INIT_VECTOR);
        return license;
    }

    /**
     * 生成证书
     *
     * @return
     */
    public static Boolean generateLicense(String licenseCode) {
        File f = null;
        try {
            f = org.springframework.util.ResourceUtils.getFile("license");
        } catch (Exception e) {
            BizLogger.error(e);
        }
        if (null == f) {
            return false;
        }
        try (OutputStream output = new FileOutputStream(f, true);) {
            String decry = AESUtil.decrypt(licenseCode, AESUtil.INIT_VECTOR);
            String[] names = decry.split(":");
            BizLogger.info("hostName:" + names[0] + "mac:" + names[1] + "startTime:" + names[2]);
            String endTime = TimeUtils.addLicense(names[2], 12);
            String str = AESUtil.encrypt(names[0] + ":" + names[1] + ":" + names[2] + ":" + endTime, AESUtil.INIT_VECTOR);

            output.write(str.getBytes(StandardCharsets.UTF_8));
            output.flush();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static String initLicense() {
        File f = null;
        try {
            f = org.springframework.util.ResourceUtils.getFile("license");
        } catch (Exception e) {
            BizLogger.error(e);
        }
        if (null == f) {
            return "";
        }
        try (InputStream inputStream = new FileInputStream(f);) {
            return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        } catch (Exception e) {
            return "";
        }
    }
}
